<template>
    <div>
        <!-- 模态框 -->
        <el-dialog title="添加角色" :visible.sync="thistype" @close='fun(tname)'>
            <el-form ref="form" :model="form" label-width="80px" v-if='deptidarr'>
                <!-- 角色名称 -->
                 <el-form-item label="角色名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <!-- 角色编码 -->
                <el-form-item label="角色编码">
                    <el-input v-model="form.tips"></el-input>
                </el-form-item>
                <!-- 部门id -->
                <el-form-item label="部门id">
                    <el-cascader :options="deptidarr" v-model="form.deptid"  :show-all-levels="false" :props="props"></el-cascader>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="upuser">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 模态框 结束 -->
    </div>
</template>
<script>
import {http,deptlist,role} from '../../api/api.js'
export default {
    props:['type','fun','tname'],
    data() {
        return {
            thistype:this.type,//模态框状态
            form:{//数据
                tips:'',//角色编码
                name:'',//角色名称
                deptid:'',//部门id
            },
            deptidarr:'',//部门信息
            props:{//级联选择的配置
                value:'id',
                label:'simplename',
                children:'children'
              }
        }
    },
    mounted() {
        this.getdeptlist()
    },
    methods: {
        //添加用户
        upuser(){
            var type=true
            for(var i in this.form){
                if(this.form[i]==''){
                    type=false
                }
            }
            if(type){

                //处理部门id
                this.form.deptid=this.form.deptid[this.form.deptid.length-1]
                //账号ID
                this.form.pid=this.$store.state.user.info.profile.id
                //排序
                this.form.num=0
                this.$http.post(http+role,JSON.stringify(this.form),{emulateJSON:true}).then((data)=>{
                if(data.data.msg=='成功'){
                    this.thistype=false
                    for(var i in this.form){
                        this.form[i]=''
                    }
                }else{
                    this.$message.error(data.data.msg)
                }
            },(err)=>{
                //location.href='./login.html'
                 this.$message.error(err.data.message)
                })
                delete this.form.pid
                delete this.form.num
            }else{
                this.$message.error('请补全信息');
            }

        },
        //处理部门信息
        childrennull(data){
            for(var i=0;i<data.length;i++){
                if(data[i].children.length==0){
                    data[i].children=null
                }else{
                    this.childrennull(data[i].children)
                }
            }
            return data
        },
        //获取部门信息
        getdeptlist(){
            this.$http.get(http+deptlist).then((data)=>{
               if(data.data.msg=='成功'){
                    this.deptidarr= this.childrennull(data.data.data)
               }else{
                   this.$message.error(err.data.msg)
               }
               
            },(err)=>{
                this.$message.error(err.data.message)
            })
        }
    },
    watch:{
        type(){
            this.thistype=this.type
        }
    }
}
</script>
<style lang="">
    
</style>